import sys
from pathlib import Path

file = Path(__file__).resolve()
parent, top = file.parent, file.parents[2]

sys.path.append(str(top))
try:
    sys.path.remove(str(parent))
except ValueError: # Already removed
    pass

__package__ = 'src.model'

from ..utils.ids import create_id
from sqlalchemy import Column, String, BigInteger, CHAR, Integer
from ..utils.database import Base, engine, SessionLocal
from sqlalchemy.dialects.mysql import LONGTEXT


class AI(Base):
    __tablename__ = 'tb_ai'
    __table_args__ = {'comment': 'AI 服务器表', 'mysql_charset': 'utf8mb4', 'extend_existing': True}
    id = Column(BigInteger, primary_key=True, autoincrement=True, comment='id')
    ai_code = Column(String(32), nullable=False, default=create_id, comment='aoi code')
    # 192.168.23.12
    hostname = Column(String(20), comment='AI 服务器 ip 地址')
    port = Column(String(10), server_default='3306', comment='AI 服务器 MySQL 端口')
    # administrator
    username = Column(String(20), server_default='root', comment='AI 服务器 MySQL 用户名')
    # 123456
    password = Column(String(20), server_default='123456', comment='AI 服务器 MySQL 密码')
    db = Column(String(20), server_default='jduan', comment='AI 服务器 MySQL 使用的仓库名')
    del_flag = Column(CHAR(1), nullable=False, server_default='0', comment="是否删除, 1-已删除, 0-未删除")
    ai_name = Column(String(20), comment='AI 服务器名')


class User(Base):
    __tablename__ = 'tb_user'
    __table_args__ = {'comment': '用户表', 'mysql_charset': 'utf8mb4', 'extend_existing': True}
    id = Column(BigInteger, primary_key=True, autoincrement=True, comment='id')
    user_code = Column(String(32), nullable=False, default=create_id, comment='user code')
    user_name = Column(String(32), nullable=False, comment='user 名')
    passwd = Column(LONGTEXT, nullable=False, comment='user 名')
    role_code = Column(String(32), nullable=False, comment='用户角色 code, 用于关联 role 表')


class Role(Base):
    __tablename__ = 'tb_role'
    __table_args__ = {'comment': '角色表', 'mysql_charset': 'utf8mb4', 'extend_existing': True}
    id = Column(BigInteger, primary_key=True, autoincrement=True, comment='id')
    role_code = Column(String(32), nullable=False, default=create_id, comment='角色 code')
    role_name = Column(String(32), nullable=False, comment='角色名')


class JudgmentFailure(Base):
    __tablename__ = 'judgment_failure'
    __table_args__ = {'comment': '缺陷表', 'mysql_charset': 'utf8mb4', 'extend_existing': True}

    id = Column(BigInteger, primary_key=True, autoincrement=True, comment='id')
    failure_name = Column(String(32), comment='缺陷名称')
    failure_code = Column(String(10), nullable=False, comment='缺陷 Code')
    frequency = Column(Integer, nullable=False, default=0, comment='缺陷频率')


if __name__ == "__main__":
    # # 创建表
    Base.metadata.create_all(bind=engine)

    # 添加 tb_ai 服务器数据
    with SessionLocal() as session:
        # role_names = ["管理员", "工程师", "操作员"]
        # for role_name in role_names:
        #     role = Role(role_name=role_name)
        #     session.add(role)

        # user = User(user_name="admin", passwd="admin", role_code="20241212-135141-ebf8")
        # session.add(user)
        # session.commit()

        # ips = [["172.168.0.246", "AIQD246"], ["172.168.0.203", "AIQD203"], ["172.168.0.204", "AIQD204"], ["172.168.0.202", "AIQD202"]]

        # for ip in ips:
        #     ai = AI(hostname=ip[0], ai_name=ip[1])
        #     session.add(ai)
        # session.commit()
        codes = [
            "P205", "P206", "P301", "P302", "P303", "P304", "P305", "P401", "P402",
            "P403", "P404", "P501", "P502", "P503", "P504", "PA01", "PA02", "PA03",
            "PA04", "PA05", "PB01", "PB02", "PB03", "PB04", "PB11", "PB12", "PC04",
            "PF01", "PF02", "PF03", "PF04", "PG01", "PG02", "PH01", "PJ01", "PJ02",
            "PJ03", "PJ04", "PQ01", "PQ02", "PQ03", "PQ04", "PQ05", "PQ06", "PQ07",
            "PQ08", "PS02", "PS05", "PU01", "PU02", "PU03", "PU04", "PU05", "PU06",
            "PU07", "PU08", "PU09", "PU10", "PU11", "PU12"
        ]

        issues = [
            "全画面异常", "画面部分异常", "open竖", "open横", "竖线", "横线", "cross short",
            "辉点", "2连续辉点", "辉点块", "辉点间距离", "灭点", "2连续灭点", "灭点块", "灭点间距离",
            "cell内划伤", "cell内异物", "微小异物", "注入口异物", "异物GAP不良", "黑点", "白点",
            "黑色差", "白色差", "周边GAP", "GAP色差", "前工程气泡", "研磨色差(表)", "研磨色差(里)",
            "pit划伤(表)", "pit划伤(里)", "CF崩缺", "CF色差", "大板元不良", "表面划伤(表)",
            "表面划伤(里)", "表面污(表)", "表面污(里)", "横边破裂崩缺", "竖边破裂崩缺", "TFT端子破裂崩缺",
            "CF端子破裂崩缺", "角崩缺", "注入口部的破裂崩缺", "进行性破裂", "角部进行性破裂", "号码机不良",
            "后工程装置故障", "上偏光板异物", "下偏光板异物", "上偏光板气泡", "下偏光板气泡", "上偏光板偏移",
            "下偏光板偏移", "上偏光板划伤", "下偏光板划伤", "上偏光板凹", "上偏光板凸", "下偏光板凹",
            "下偏光板凸"
        ]

        for i in range(len(codes)):
            code = codes[i]
            issue = issues[i]

            failure_item = JudgmentFailure(failure_name=issue, failure_code=code)
            session.add(failure_item)
        session.commit()



